/*
 * Name: Minh-Tuan ta
 */

package data;

import java.util.ArrayList;
import beans.*;

/*
 * Author: Minh-Tuan Ta
 * Date: 11/6/11
 * Class: PostDB
 * This class is the data access class for posts
 */
public class PostDB extends ContentDB {
	
	
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getPosts
	 * This method will get all the posts in the database
	 */
	public static synchronized ArrayList<Post> getPosts()
	{
		return convertContentsToPosts(getAllContentOfContenttype(ContentTypeDB.getContentTypeID("Post")));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getPostsOfUser
	 * This method will get all the posts of a specific user in the database
	 */
	public static synchronized ArrayList<Post> getPostsOfUser(int userid)
	{
		return convertContentsToPosts(getContentsOfUserAndType(userid, ContentTypeDB.getContentTypeID("Post")));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getPost
	 * This method will get a post from the database giving the specific post id
	 */
	public static synchronized Post getPost(int postid)
	{
		return convertContentToPost(getContent(postid));
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: addPost
	 * This method will add a new post
	 */
	public static synchronized int addPost(Content post)
	{
		return addContent(post);
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: upadtePost
	 * This method will add a new post
	 */
	public static synchronized int updatePost(Content post)
	{
		return updateContent(post);
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: deletePost
	 * This method will delete a post
	 */
	public static synchronized int deletePost(Content post)
	{
		//delete comments first
		ArrayList<Comment> comments = CommentDB.getCommentsForContent(post.getContentID());
		for (int i = 0 ; i < comments.size(); i++)
		{
			CommentDB.deleteComment(comments.get(i));
		}
		
		return deleteContent(post);
	}
}
